package com.java2007.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.dao.AdminDao;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.service.AdminService;
import com.java2007.hotel.utils.MD5Utils;

import java.sql.SQLException;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-25
 */
public class AdminServiceImpl implements AdminService {

    private AdminDao adminDao = (AdminDao) BeanFactory.getBean("com.java2007.hotel.dao.impl.AdminDaoImpl");

    /**
     * 登录
     *
     * @param adminName
     * @param password
     * @return
     */
    @Override
    public Admin login(String adminName, String password) throws SQLException {

        try {
            if (StringUtils.isEmpty(adminName) || StringUtils.isEmpty(password)) {
                throw new RuntimeException("用户名跟密码都不能为空");
            }

            Admin admin = adminDao.findByAdminName(adminName.trim());

            if(admin == null) {
                throw new RuntimeException("用户名跟密码正确");
            }

            //加密的密码  dbPwd
            String dbPwd = admin.getPassword();

            //明文密码 password
            if(dbPwd.equals(MD5Utils.md5(password))) {
                return admin;
            } else {
                throw new RuntimeException("用户名跟密码正确");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("用户名跟密码正确");
        }
    }
}
